﻿@using System.Data
@using System.Text
@using KRM.Core
@model vWebContactSupplierCompany
@{
    ViewBag.Title = "Send Message To Supplier";
    Layout = "~/Views/Shared/_PopupLayout.cshtml";
    var db = new KRMEntities();
    int orderid = ViewBag.orderid;
    var order = db.vWebContactSupplierOrders.FirstOrDefault(a => a.OrderID == orderid) ?? new vWebContactSupplierOrder();
    var options = db.vWebContactSupplierOptions.Where(a => a.OrderID == orderid).ToList();
    var orderitems = db.tblOrderItems.Where(a => a.OrderID == orderid && a.CompanyID == Model.CompanyID);
    var message = db.vWebContactSupplierMessages.Where(a => a.OrderID == orderid && a.CompanyID == Model.CompanyID).ToList();
    
    var sb = new StringBuilder();
    sb.Append("To " + Model.CompanyName + ",<br/>");
    sb.Append("Order Ref: " + order.OrderRef + "<br/>" +
                "Name: " + order.Name + "<br/>");
    sb.Append("cancel" + "<br/>" + "<br/>");
    sb.Append("**************************************************" + "<br/>");
    int ItemNumber = 1;
    bool OrderCancelled = (order.OrderCancelled == 1);
    foreach (var item in orderitems)
    {
        sb.Append("Product " + ItemNumber + "<br/>");
        sb.Append("---------------" + "<br/>");
        //				if (tnItem.Checked)
        //				{
        if (OrderCancelled || (item.ItemCancelled || (order.OrderCancelled == 1)))
        {
            sb.Append("[Cancelled]: ");
        }
        else
        {
            sb.Append("[Active]:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        sb.Append(item.ProductName + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- [" + item.ProductOption + "]" + "<br/>");
        //Add SubOptions
        int SONum = 1;
        foreach (var tnSO in options.Where(a => a.OrderItemID == item.OrderItemID))
        {
            sb.Append("Opt&nbsp;&nbsp;" + SONum.ToString() + ":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            if (OrderCancelled || (item.ItemCancelled || (order.OrderCancelled == 1)))
            {
                sb.Append("[Cancelled]:&nbsp;&nbsp;");
            }
            else
            {
                sb.Append("[Active]:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            }
            sb.Append(tnSO.ProductSubOptionItem.Replace(", ", ("<br/>" + "		")).Replace(":", ":	") + "<br/>");
            SONum++;
        }
        //				}
        sb.Append("<br/>" + "**************************************************" + "<br/>");
        ItemNumber++;
    }
    //sb.Append("<br/>" + "**************************************************" + "<br/>");
    sb.Append("Order Details" + "<br/>" + "***************" + "<br/>");
    DateTime timePlaced = DateTime.Parse(order.DatePlaced.ToString());
    sb.Append("DatePlaced:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + DateTime.Parse(timePlaced.ToLongDateString()) + "<br/>");
    sb.Append("Shipping Address:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "<br/>");
    sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + order.ShippingDetails.Replace("\n", "<br/>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + "<br/>");

    sb.Append("**************************************************");


    var dtDisplayHistory = new DataTable();
    dtDisplayHistory.Columns.Add("CompanyMessageID", Type.GetType("System.Int32"));
    dtDisplayHistory.Columns.Add("MessageContent", Type.GetType("System.String"));
    //Add Current Row
    string strContent = "0 - NEW! - [New Message] - [Not Yet Sent!]";
    object[] objCurrent = new object[2];
    objCurrent[0] = "0";
    objCurrent[1] = strContent;
    dtDisplayHistory.Rows.Add(objCurrent);

    if (message.Any())
    {
        int num = 1;
        foreach (var item in message)
        {
            //Current Message - SEE Collection Message! below for specific details to display
            //ACK - [General] - [Waltons] - [Sent 4 Times] - [Created 10:23am 05/08/2004]- [Last Sent 15:45am 09/08/2004]

            var Ack = item.Ack;
            bool Resend = (item.ResendEvery != "Never");
            string Status = "NEW ";
            if ((Ack == true) && (Resend))
            {
                //Resend=1, ACK=1
                Status = "ACKD";
            }
            else if (Ack == true)
            {
                //Resend=0, ACK=1
                Status = "SENT";
            }
            else if (Resend)
            {
                //Resend=1, ACK=0
                Status = "WAIT";
            }
            else
            {
                //Resend=0, ACK=0
                Status = "NEW!";
            }

            string strLastSent = "Never";
            if (!string.IsNullOrEmpty(item.DateLastSent.ToString()))
            {
                DateTime LastSent = (DateTime)item.DateLastSent;
                strLastSent = LastSent.ToShortDateString() + " " + LastSent.ToShortTimeString();
            }
            DateTime Created = (DateTime)item.DateCreated;
            string SentTimes = (item.TimesSent == 0) ? "0" : item.TimesSent.ToString();
            strContent = num.ToString() + " - " + Status + " - [" + item.MessageType + "] - [Sent " + SentTimes +
                " Times] - [Created " + Created.ToShortDateString() + " " + Created.ToShortTimeString() + "] - [Last Sent " +
                 strLastSent + " ]";
            object[] objRow = new object[2];
            objRow[0] = item.CompanyMessageID;
            objRow[1] = strContent;
            //objRow[2] =  (item.Ack == "1") ? "1" : "0";
            dtDisplayHistory.Rows.Add(objRow);
            num++;
        }
    }
    DataView dvItems = dtDisplayHistory.DefaultView;
}
<link href="~/css/jquery.treeview.css" rel="stylesheet" />
<script src="~/js/jquery.treeview.min.js"></script>
<script>
    $(document).ready(function () {
        $("#tree").treeview({
            animated: "fast",
            collapsed: true,
        });
    });
</script>
<form action="/orderview/contactcustomer" method="POST" style="padding: 5px">
    <div style="width: 50%; float: left">
        <div class="customer-box">
            <p class="customer-box-title">Order Information</p>
            <div class="customer-box-bg">
                <table>
                    <tr>
                        <td>Order: </td>
                        <td><strong>@order.OrderRef</strong></td>
                    </tr>
                    <tr>
                        <td>Name: </td>
                        <td><strong>@order.Name</strong></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="customer-box">
            <p class="customer-box-title">Company</p>
            <div class="customer-box-bg">
                @Html.DropDownList("CompanyID", null, new { @class = "input-block-level", onchange = "companychange("+Request["orderid"]+",this.value)" })
                <table>
                    <tr>
                        <td>@(Model.IsEmail == true ? "Email" : "Fax"): </td>
                        <td><strong>@(Model.IsEmail == true ? Model.Email : Model.Fax)</strong></td>
                    </tr>
                    <tr>
                        <td>Contact: </td>
                        <td><strong>@Model.Contact  </strong></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="customer-box">
            <div class="customer-box-title">
                <div class="row-fluid">
                    <div class="span4">Message Type</div>
                    <div class="span8">
                        <select class="input-block-level" name="messagetype">
                            <option value="1">General</option>
                            <option value="2">Cancellation</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="customer-box">
            <div class="customer-box-title">
                <div class="row-fluid">
                    <div class="span4">Return Address</div>
                    <div class="span8">
                        <select class="input-block-level" name="returnaddress">
                            <option value="Care@Kybotech.co.uk">Care@Kybotech.co.uk</option>
                            <option value="Cancellations@Kybotech.co.uk">Cancellations@Kybotech.co.uk</option>
                            <option value="@Html.Raw(Environment.UserName + "@kybotech.co.uk")">@Html.Raw(Environment.UserName + "@kybotech.co.uk")</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div style="width: 50%; float: right">
        <div class="customer-box">
            <p class="customer-box-title">Details</p>
            <div class="customer-box-bg">
                <div class="contact-tree-bg">
                    <ul id="tree">
                        @foreach (var item in orderitems)
                        {
                            var color = ((item.ItemCancelled || (order.OrderCancelled == 1)) ? "style=background:Pink" : "");
                            <li><span @color>
                                <input type="checkbox" checked="checked" class="pull-left" style="margin-right: 5px" />@item.ProductName</span>
                                <ul>
                                    <li>
                                        <span @color>
                                            <input type="checkbox" checked="checked" class="pull-left" style="margin-right: 5px" />@item.ProductOption</span>
                                        @if (options.Any())
                                        {
                                            var subs = options.Where(a => a.OrderItemID == item.OrderItemID).ToList();
                                            if (subs.Any())
                                            {
                                            <ul>
                                                @foreach (var sub in subs)
                                                {
                                                    <li>
                                                        <span @color>
                                                            <input type="checkbox" checked="checked" class="pull-left" style="margin-right: 5px" />@sub.ProductSubOptionItem</span>
                                                    </li>
                                                }
                                            </ul>
                                            }

                                        }
                                        else
                                        {
                                            if (!string.IsNullOrEmpty(item.ProductSubOptions))
                                            {
                                            <ul>
                                                <li>
                                                    <span @color>
                                                        <input type="checkbox" checked="checked" class="pull-left" style="margin-right: 5px" />@item.ProductSubOptions</span>
                                                </li>
                                            </ul>
                                            }
                                        }
                                    </li>
                                </ul>
                            </li>
                        }
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            <div class="customer-box">
                <p class="customer-box-title">New message</p>
                <div class="customer-box-bg">
                    <div class="email-content">
                        @Html.Raw(sb)
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="customer-box">
        <div class="customer-box-title">
            <div class="row-fluid">
                <div class="span3">History</div>
                <div class="span6">
                    <select class="input-block-level">
                        @foreach (DataRowView item in dvItems)
                        {
                            <option value="@item["CompanyMessageID"]">@item["MessageContent"]</option>
                        }
                    </select>
                </div>
                <div class="span3 text-right">
                    <button class="btn" disabled="disabled" type="button">Acknowledged!</button></div>
            </div>
        </div>
    </div>
    <div class="customer-box">
        <div class="customer-box-title">
            <div class="row-fluid">
                <div class="span3">Resend Automatically</div>
                <div class="span6">
                    <select class="input-block-level" name="resend">
                        <option value="0">Never</option>
                        <option value="1">OnceADay</option>
                        <option value="2">TwiceADay</option>
                    </select>
                </div>
                <div class="span3 text-right">
                    <button class="btn" type="button" onclick="windowclose();">Cancel</button>
                    <button class="btn btn-primary" type="submit">Send Fax</button>
                </div>
            </div>
        </div>
    </div>
</form>
